<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 登录表单区域 -->
      <p>电商后台管理系统</p>
      <el-form class="login_form" ref="loginFormRef" :model="loginForm" :rules="loginFormRules">
        <div>登录</div>
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input prefix-icon="el-icon-user" v-model="loginForm.username" autofocus="true"></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input prefix-icon="el-icon-lock" v-model="loginForm.password" show-password></el-input>
        </el-form-item>
        <el-form-item>
          <el-checkbox v-model="remeberLoginStatus">记住账号</el-checkbox>
        </el-form-item>
        <el-form-item class="login_btn">
          <el-button type="primary" @click="login">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="copyright">
      <span>Copyright © 2018 - 2020 微淘(weitao.com) -</span>
      <a href="http://www.beian.miit.gov.cn/" target="_blank">豫ICP备18003915号-1</a>
      <img src="../assets/beian.png" alt />
      <a
        href="http://www.beian.gov.cn/portal/registerSystemInfo?recordcode=41019702002723"
        target="_blank"
      >豫公网安备 41019702002723号</a>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loginForm: {
        username: "admin",
        password: "123456"
      },
      loginFormRules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 4, max: 12, message: "长度在4到12个字符", trigger: "blur" }
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 6, max: 18, message: "长度在6到18个字符", trigger: "blur" }
        ]
      },
      remeberLoginStatus: true
    };
  },
  methods: {
    resetLoginForm() {
      this.$refs.loginFormRef.resetFields();
    },
    login() {
      this.$refs.loginFormRef.validate(async valid => {
        // console.log(valid);
        if (!valid) return;

        const { data: res } = await this.$http.post("login", this.loginForm);
        if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
        this.$message.success("登录成功");
        //1.将登录成功之后的token，保存到客户端的sessionStorage中
        // 1.1 项目中除了登录之外的其他API接口，必须在登录之后才能访问
        // 1.2 token只应在当前网站打开期间生效，所以将token保存在sessionStorage中
        console.log(res);
        window.sessionStorage.setItem("token", res.data.token);
        //2.通过编程式导航跳转到后台主页，路由地址是/home
        this.$router.push("/home");
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.login_container {
  position: relative;
  width: 100%;
  overflow: hidden;
  min-height: 100vh;
  background-color: #f0f2f5;
  background-image: url();
  background-position: 50%;
  background-repeat: no-repeat;
}
.login_box {
  position: absolute;
  left: 50%;
  top: 46%;
  transform: translate(-50%, -50%);
  width: 800px;
  height: 600px;
  border-radius: 10px;
  box-shadow: 0 0 20px rgba(0, 0, 0, 0.08);
  background: url("../assets/bg.jpg") no-repeat;
  & > p:nth-child(1) {
    position: absolute;
    left: 70px;
    top: 50%;
    transform: translateY(-50%);
    margin: 0;
    font-size: 20px;
    color: #fff;
  }
}
.login_form {
  border-radius: 0 10px 10px 0;
  margin-left: 300px;
  padding: 100px 60px;
  height: 600px;
  box-sizing: border-box;
  background-color: #ffffff;
  & > div:nth-child(1) {
    text-align: center;
    font-size: 16px;
    // font-weight: 600;
    letter-spacing: 6px;
    margin-bottom: 90px;
    color: #777;
  }
}
.login_btn {
  text-align: center;
  .el-button {
    width: 160px !important;
    border-radius: 36px;
  }
}
.copyright {
  position: absolute;
  bottom: 30px;
  left: 50%;
  display: flex;
  align-items: center;
  transform: translateX(-50%);
  font-size: 13px;
  color: #999;
  a {
    margin: 0 4px;
    color: #999;
    text-decoration: none;
    // line-height: 24px;
  }

  p {
    margin-bottom: 8px;
    span {
      margin: 0 6px;
      cursor: pointer;
    }
  }
}
</style>
